/**
 * 消息黑名单
 */
package labox.innovation.gameserver.model;

import java.util.Set;

import javolution.util.FastSet;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * This class ...
 *
 * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
 */

public class BlockList {
	
	private final Set<String> _blockSet;

	private boolean _blockAll;

	public BlockList() {
		_blockSet = new FastSet<String>();
		_blockAll = false;
	}

	private synchronized void addToBlockList(FuzePcInstance character) {
		if (character != null) {
			_blockSet.add(character.getName());
		}
	}

	private synchronized void removeFromBlockList(FuzePcInstance character) {
		if (character != null) {
			_blockSet.remove(character.getName());
		}
	}

	private boolean isInBlockList(FuzePcInstance character) {
		return _blockSet.contains(character.getName());
	}

	private boolean isBlockAll() {
		return _blockAll;
	}

	public static boolean isBlocked(FuzePcInstance listOwner, FuzePcInstance character) {
		BlockList blockList = listOwner.getBlockList();
		return blockList.isBlockAll() || blockList.isInBlockList(character);
	}

	private void setBlockAll(boolean state) {
		_blockAll = state;
	}

	private Set<String> getBlockList() {
		return _blockSet;
	}

	public static void addToBlockList(FuzePcInstance listOwner, FuzePcInstance character) {
		listOwner.getBlockList().addToBlockList(character);

		SystemMessage sm = new SystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);
		sm.addString(listOwner.getName());
		character.sendPacket(sm);

		sm = new SystemMessage(SystemMessageId.S1_WAS_ADDED_TO_YOUR_IGNORE_LIST);
		sm.addString(character.getName());
		listOwner.sendPacket(sm);
	}

	public static void removeFromBlockList(FuzePcInstance listOwner, FuzePcInstance character) {
		listOwner.getBlockList().removeFromBlockList(character);

		SystemMessage sm = new SystemMessage(SystemMessageId.S1_WAS_REMOVED_FROM_YOUR_IGNORE_LIST);
		sm.addString(character.getName());
		listOwner.sendPacket(sm);
	}

	public static boolean isInBlockList(FuzePcInstance listOwner, FuzePcInstance character) {
		return listOwner.getBlockList().isInBlockList(character);
	}

	public static boolean isBlockAll(FuzePcInstance listOwner) {
		return listOwner.getBlockList().isBlockAll();
	}

	public static void setBlockAll(FuzePcInstance listOwner, boolean newValue) {
		listOwner.getBlockList().setBlockAll(newValue);
	}

	public static void sendListToOwner(FuzePcInstance listOwner) {
		for (String playerName : listOwner.getBlockList().getBlockList()) {
			listOwner.sendMessage(playerName);
		}
	}
}
